package com.nifostasky.main.terminal;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 使用Runtime.exec()形式执行命令
 */
public class RuntimeExecCase {

    public static Boolean runtimeExecCase(){
        String command = "ping www.baidu.com";

        try{
            Process p = Runtime.getRuntime().exec(command);

            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String outInfo;
            while ((outInfo = reader.readLine()) != null) {
                System.out.println(outInfo);
            }

            // 等待process子进程终止
            p.waitFor();

            //exitValue==0 时表示执行成功; 否则表示执行失败
            return p.exitValue() == 0;

        }catch (Exception e){
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println("\n"+"=======================================================================================================================");
        RuntimeExecCase.runtimeExecCase();
        System.out.println("======================================================================================================================="+"\n");

    }
}
